﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace EmoneyWeiXinCommon
{
    public class StringCommon
    {
        public static void CreateFile(string filename)
        {
            if (System.IO.Directory.Exists(filename) == false)//如果不存在就创建file文件夹 
            {
                System.IO.Directory.CreateDirectory(filename);
            }
        }

        public static string RepFile(string filename)
        {
            if (!string.IsNullOrEmpty(filename))
            {
                filename = filename.Replace(" ", "");
                filename = filename.Replace("*", "");
                filename = filename.Replace("/", "");
                filename = filename.Replace(":", "");
                filename = filename.Replace("?", "");
                filename = filename.Replace(">", "");
                filename = filename.Replace("<", "");
                return filename;
            }
            return "";
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="Path">路径</param>
        public static void FileDel(string Path)
        {
            if (System.IO.File.Exists(Path))
            {
                System.IO.File.Delete(Path);
            }
        }

        public static bool SaveNewFile(string oldPath, string newPath)
        {
            if (oldPath != newPath)
            {
                FileDel(newPath);
                File.Copy(oldPath, newPath);
                File.Delete(oldPath);
            }
            return true;
        }

        public static bool SaveFile(byte[] dates, string StrSavePath)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(StrSavePath, FileMode.Create, FileAccess.Write, FileShare.None);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(dates);
                bw.Dispose();
                bw.Close();
                fs.Dispose();
                fs.Close();
            }
            catch (Exception)
            {
                Thread.Sleep(100);
                SaveFile(dates, StrSavePath);
            }
            return true;
        }

        public static bool SaveFile(string dates, string StrSavePath)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(StrSavePath, FileMode.Create, FileAccess.Write, FileShare.None);
                StreamWriter bw = new StreamWriter(fs, Encoding.GetEncoding("utf-8"));
                bw.Write(dates);
                bw.Dispose();
                bw.Close();
                fs.Dispose();
                fs.Close();
            }
            catch (Exception)
            {
                Thread.Sleep(100);
                SaveFile(dates, StrSavePath);
            }
            return true;
        }

        public static string GetFile(string file)
        {
            string str = "";
            //异常检测开始
            try
            {
                FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);//读取文件设定
                StreamReader m_streamReader = new StreamReader(fs, Encoding.GetEncoding("utf-8"));//设定读写的编码
                //  从数据流中读取每一行，直到文件的最后一行，并在rTB_Display.Text中显示出内容

                string strLine = m_streamReader.ReadLine();
                while (strLine != null)
                {
                    str += strLine;
                    strLine = m_streamReader.ReadLine();
                }
                //关闭此StreamReader对象
                m_streamReader.Close();
            }
            catch (Exception ex)
            {
                //抛出异常
                return ex.Message;
            }
            //异常检测结束
            return str;
        }
    }
}
